﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

using BitmapTagger;

namespace ImageTagEditor.ValueConverters
{
	/// <summary>
	/// Value converter for a string to Rational and back
	/// </summary>
	public class RationalValueConverter : IValueConverter
	{

		/// <summary>
		/// Converts a Rational to a string
		/// </summary>
		/// <param name="value">The rational to convert</param>
		/// <param name="targetType">Not Used</param>
		/// <param name="parameter">Not Used</param>
		/// <param name="culture">Not Used</param>
		/// <returns>The rational as a string</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null)
				return "";
			if (value is Rational)
			{
				Rational rational = value as Rational;

				if(rational.Denominator != 1)
					return rational.ToString();
				else
					return rational.Numerator.ToString();
			}
			throw new Exception("RationalValueConverter can only convert from a Rational");
		}

		
		/// <summary>
		/// Converts a string to a Rational
		/// </summary>
		/// <param name="value">The string to convert</param>
		/// <param name="targetType">Not Used</param>
		/// <param name="parameter">Not Used</param>
		/// <param name="culture">Not Used</param>
		/// <returns>The Rational from the string</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value is string)
			{
				string rationalString = value as string;

				if (string.IsNullOrEmpty(rationalString))
					return null;

				Rational rational = new Rational(rationalString);
				return rational;
			}
			throw new Exception("RationalValueConverter can only converter back from a string");
		}
	}
}
